<?php
/**
 * 展示系统所有的用户订单
 */
use yii\helpers\Url;

$web = Url::base();
$scriptUrl = \Yii::$app->request->scriptUrl;
?>


<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group">
                <a class="btn btn-default " type="button" data-toggle="modal" href="#orderImportModal"><i
                        class="fa fa-edit"></i> 导入用户订单</a>
            </div>
        </div>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">

            <div class="form-group col-lg-3">
                <div class="input-group input-large custom-date-range" data-date="13/07/2013"
                     data-date-format="mm/dd/yyyy">
                    <input name="from" id="start_time" class="form-control dpd1" type="text" placeholder="开始日期"
                           value="<?= $start_time ?>">
                    <span class="input-group-addon">To</span>
                    <input name="to" id="end_time" class="form-control dpd2" type="text" placeholder="结束日期"
                           value="<?= $end_time ?>">
                </div>

            </div>

            <div class="form-group col-lg-2">
                <select id="order_status" class="form-control" onchange="" style="width: 100%">
                    <option value="">请选择订单状态</option>
                    <option value="11">待确认订单</option>
                    <option value="20">订单已同步</option>
                    <option value="21">出库中</option>
                    <option value="22">配送中</option>
                    <option value="30">已完成</option>
                    <option value="31">已取消</option>
                </select>
            </div>

            <div class="form-group col-lg-3">
                <div class="input-group m-bot15">
                    <div class="input-group-btn">
                        <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">
                            <span id="search_field_name" data-field="<?= $field ?>">搜索字段</span>
                            <span class="caret"></span></button>
                        <ul class="dropdown-menu">
                            <li><a href="javascript:setOrderField('','搜索字段')">搜索字段</a></li>
                            <li><a href="javascript:setOrderField('code','订单号码')">订单号码</a></li>
                            <li><a href="javascript:setOrderField('user_name','会员姓名')">会员姓名</a></li>
                            <li><a href="javascript:setOrderField('receiver_name','收货姓名')">收货姓名</a></li>
                            <li><a href="javascript:setOrderField('receiver_phone','电话号码')">电话号码</a></li>
                            <li><a href="javascript:setOrderField('goods_name','商品名称')">商品名称</a></li>
                            <li><a href="javascript:setOrderField('goods_code','商品代码')">商品代码</a></li>
                        </ul>
                    </div>
                    <input class="form-control" type="text" id="field_value" value="<?= $field_value ?>">
                </div>
            </div>
            <div class="form-group col-lg-3">
                <div id="searchInstitution"></div>
                <div id="department_div">
                    <div id="inputdepartment"></div>
                </div>
            </div>
            <button id="btnSearch" class="btn btn-primary"><i class="fa fa-search"></i> 搜索</button>
        </div>
    </div>
</div>

<div class="panel">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th>订单编号</th>
                    <th>所属机构</th>
                    <th>下单用户</th>
                    <th>订单总金额</th>
                    <th>支付积分</th>
                    <th>支付备注</th>
                    <th>订单状态</th>
                    <th>下单时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                foreach ($items as $item) {
                    ?>
                    <tr>
                        <td><?= $item['code'] ?></td>
                        <td><?= $item['institution_name'] ?></td>
                        <td><?= $item['user_name'] ?></td>
                        <td>￥<?= $item['total_selling_price'] ?></td>
                        <td><?= $item['integral'] ?></td>
                        <td><?= $item['pay_remark'] ?></td>
                        <td>
                            <?php
                            $statusCss = 'label label-default';
                            switch ($item['status']) {
                                case 11:  // 待确认订单
                                    $statusCss = 'label label-warning';
                                    break;
                                case 21: // 出库中
                                case 22: // 配送中
                                    $statusCss = 'label label-info';
                                    break;
                                case 30: // 已完成
                                    $statusCss = 'label label-success';
                                    break;
                                case 31: // 已取消
                                case 32: // 已失效
                                    $statusCss = 'label label-disable';
                                    break;
                            }
                            ?>
                            <label class="<?= $statusCss ?>"><?= $item['status_name'] ?></label>
                        </td>
                        <td><?= $item['create_datetime'] ?></td>
                        <td>
                            <div class="btn-group">
                                <a class="btn btn-link" data-toggle="modal" data-order-id="<?= $item['id'] ?>"
                                   data-order-code="<?= $item['code'] ?>" href="#orderDetailModal">订单详情</a>
                                <?php
                                if (\app\models\Permission::isSystemAdmin()) {
                                    ?>
                                    <a class="btn btn-link" data-toggle="modal" data-order-code="<?= $item['code'] ?>"
                                       href="#orderFlowModal">订单流转</a>
                                    <?php
                                }
                                ?>

                                <?php
                                switch ($item['status']) {
                                    case 1:
                                    case 2:
                                    case 3:
                                    case 9:
                                    case 10:
                                    case 11:
                                        ?>
                                        <button class="btn btn-link" onclick="cancelOrder('<?= $item['code'] ?>')">
                                            取消订单
                                        </button>
                                        <?php
                                        break;
                                }
                                ?>
                            </div>
                        </td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
        </div>
        <div class="pull-left">
            <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
        </div>
    </div>
</div>

<div aria-hidden="true" role="dialog" tabindex="-1" id="orderDetailModal" class="modal fade">
    <div class="modal-dialog modal-full">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">订单详情</h4>
            </div>
            <div class="modal-body" id="order_detail_content">
                正在加载数据....
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<div aria-hidden="true" role="dialog" tabindex="-1" id="orderFlowModal" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">订单流转信息</h4>
            </div>
            <div class="modal-body" id="order_flow_content">
                正在加载数据....
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<!--导入用户订单-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="orderImportModal" class="modal fade">
    <div class="modal-body" id="order_import_content">
        正在加载数据....
    </div>
</div>

<script type="application/javascript">
    var insSelect;
    var depSelect;
    $(function () {
        $('#orderImportModal').on('show.bs.modal', function (e) {
            requestHtml('order-import-partial', {}, true, function (responseHtml) {
                $("#order_import_content").html(responseHtml);
            });
        });
        $('#orderDetailModal').on('show.bs.modal', function (e) {
            $("#order_detail_content").html('正在加载数据....');
            var data = {};
            data.order_id = e.relatedTarget.attributes['data-order-id'].value;
            data.order_code = e.relatedTarget.attributes['data-order-code'].value;
            requestHtml('order-detail-partial', data, true, function (responseHtml) {
                $("#order_detail_content").html(responseHtml);
            }, function () {
                $('#orderDetailModal').modal('hide');
            });
        });

        $('#orderFlowModal').on('show.bs.modal', function (e) {
            $("#order_flow_content").html('正在加载数据....');
            var orderCode = e.relatedTarget.attributes['data-order-code'].value;
            loadOrderFlowListPartView(orderCode);
        });

        //初始化选择订单状态
        var order_status = '<?=$order_status?>';
        if (order_status != null && order_status != '') {
            $("#order_status").val(order_status);
        }

        //初始化搜索字段
        var field = '<?=$field?>';
        switch (field) {
            case "":
                $("#search_field_name").html("搜索字段");
                break;
            case "code":
                $("#search_field_name").html("订单号码");
                break;
            case "user_name":
                $("#search_field_name").html("会员姓名");
                break;
            case "receiver_name":
                $("#search_field_name").html("收货姓名");
                break;
            case "receiver_phone":
                $("#search_field_name").html("电话号码");
                break;
            case "goods_name":
                $("#search_field_name").html("商品名称");
                break;
            case "goods_code":
                $("#search_field_name").html("商品代码");
                break;
        }

        $('#btnSearch').click(function () {
            var searchInstitutionCode = $('#searchInstitution').val();
            var url = window.location.pathname;
            if (searchInstitutionCode == 0) {
//                url = removeUrlParam(url, 'institution_code');
            } else {
                url = setUrlParam(url, 'page', 1);
                url = setUrlParam(url, 'institution_code', searchInstitutionCode);
            }

            var start_time = $("#start_time").val();
            if (start_time != null && start_time != '') {
                url = setUrlParam(url, 'start_time', start_time);
            }
            var end_time = $("#end_time").val();
            if (end_time != null && end_time != '') {
                url = setUrlParam(url, 'end_time', end_time);
            }
            var order_status = $("#order_status").val();
            if (order_status != '') {
                url = setUrlParam(url, 'order_status', order_status);
            }

            var field = $("#search_field_name").attr("data-field");
            if (field != "" && field != null) {
                url = setUrlParam(url, 'field', field);
            }
            var field_value = $("#field_value").val();
            if (field_value != '') {
                url = setUrlParam(url, 'field_value', field_value);
            }
            var department = $("#inputdepartment").val();
            if (department != '') {
                url = setUrlParam(url, 'department_id', department);
            }
            window.location.href = url;
        });

        //机构列表初始化

        var insCode = "<?= $current_institution_code ?>";

        var institutions =<?=json_encode($institutionList)?>;

        $('#searchInstitution').treeAutocomplete({
            defaultText: '----请选择机构----',
            source: institutions,
            onSelected: function (element) {
                //新增子机构选择
                var department_div = $("#department_div");
                var html = "";
                html += '<div id="inputdepartment"></div>';

                department_div.html(html);
                var institution_code = element.getValue();
                var action = "<?=$scriptUrl?>" + "/common" + "/department-tree-handler";
                var data = {};
                data.institution_code = institution_code;
                requestJson(action, data, true, function (responseData) {

                    if (responseData.status == 0 && responseData.data != '') {
                        $('#inputdepartment').treeAutocomplete({
                            defaultText: "--选择子机构(部门)--",
                            isShowCode: false,
                            source: responseData.data
                        });
                        $("#department_div").css("display", "block");
                    } else {
                        $("#department_div").css("display", "none");
                    }
                });

            }
        }).setValue(insCode);

        ///子机构有缺省值，则初始化树
        if (!(insCode == '' || insCode == '0')) {

            var disabled = true;
            var department_selected = $("#department_div");
            var html_selected = "";
            html_selected += '<div id="inputdepartment"></div>';
            department_selected.html(html_selected);
            var institution_code = insCode;

            var action = "<?=$scriptUrl?>" + "/common" + "/department-tree-handler";
            var data = {};
            data.institution_code = institution_code;

            requestJson(action, data, true, function (responseData) {
                if (responseData.status == 0 && responseData.data != '') {
                    depSelect = $('#inputdepartment').treeAutocomplete({
                        defaultText: "--选择子机构(部门)--",
                        isShowCode: false,
                        source: responseData.data
                    }).setValue("<?=$selectdepartment?>");
                    $("#department_div").css("display", "block");
                } else {
                    $("#department_div").css("display", "none");
                }
            });
        }
    });

    function loadOrderFlowListPartView(orderCode) {
        requestHtml('order-flow-list-partial', {order_code: orderCode}, true, function (responseHtml) {
            if ($.trim(responseHtml).length > 0) {
                $("#order_flow_content").html(responseHtml);
            } else {
                $("#order_flow_content").html('未找到订单流转信息!');
            }
        });
    }

    function cancelOrder(orderCode) {
        var c = confirm('确认取消此订单?');
        if (c == true) {
            requestJson('cancel-order-handler', {order_code: orderCode}, true, function (responseData) {
                switch (responseData.status) {
                    case '0':
                        window.location.reload();
                        break;
                    case '20':
                        alert('未找到此订单!');
                        break;
                    case '21':
                        alert('此订单不能取消!');
                        break;
                }
            });
        }
    }


    function setOrderField(field_name, show_name) {
        $("#search_field_name").attr("data-field", field_name);
        $("#search_field_name").html(show_name);
    }
</script>

